package org.jeecg.modules.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.entity.DbCity;
import org.jeecg.modules.mapper.DbCityMapper;
import org.jeecg.modules.service.IDbCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @Description: db_baseline
 * @Author: jeecg-boot
 * @Date:   2024-06-13
 * @Version: V1.0
 */
@Service
public class DbCityServiceImpl extends ServiceImpl<DbCityMapper, DbCity> implements IDbCityService {

    @Autowired
    private DbCityMapper dbCityMapper;
    public List<DbCity> getRegions(){
        List<DbCity> cityList = dbCityMapper.selectList(new QueryWrapper<DbCity>().orderByAsc("code", "parent_code", "lev"));
        List<DbCity> resultList = new ArrayList<>();
        DbCity lv1 = null;
        DbCity lv2 = null;
        DbCity lv3 = null;
        for (DbCity city : cityList) {
            if (city.getLev() == 1) {//省级
                resultList.add(city);
                lv1 = city;
            } else if (city.getLev() == 2) {//市级
                if(lv1.getChildren()==null){
                    lv1.setChildren(new ArrayList<>());
                }
                lv1.getChildren().add(city);
                lv2 = city;
            } else if (city.getLev() == 3) {//区
                if(lv2.getChildren()==null){
                    lv2.setChildren(new ArrayList<>());
                }
                lv2.getChildren().add(city);
                lv3 = city;
            }
//            else if (city.getLev() == 4) {//街道
//                if(lv3.getChildren()==null){
//                    lv3.setChildren(new ArrayList<>());
//                }
//                lv3.getChildren().add(city);
//            }
        }

        return resultList;
    }
    /**
     * 省市区三级查询
     * @return
     */
//    public List<DbCity> getRegions() {
////        查找省级
//        List<DbCity> json=dbCityMapper.selectList(new QueryWrapper<DbCity>().eq("lev",1).eq("parent_code",0));
//        List list = new ArrayList<>();
////        市级
//        json.forEach(item->{
//            list.add(item.getCode());
//        });
//        List<DbCity> json2 = dbCityMapper.selectList(new QueryWrapper<DbCity>().eq("lev", 2).in("parent_code", list));
//        Map<String,List<DbCity>> mdv=json2.stream().collect(Collectors.groupingBy(DbCity::getParentCode));
//        json.stream().forEach(item->item.setChildren(mdv.get(item.getCode())));
//        //        县级
//        List list2 = new ArrayList<>();
//        json2.forEach(item->{
//            list2.add(item.getCode());
//        });
//        List<DbCity> json3 = dbCityMapper.selectList(new QueryWrapper<DbCity>().eq("lev", 3).in("parent_code", list2));
//        Map<String,List<DbCity>> mdv2=json3.stream().collect(Collectors.groupingBy(DbCity::getParentCode));
//        json.stream().filter(a->a.getChildren()!=null).forEach(a->a.getChildren().stream().forEach(i->i.setChildren(mdv2.get(i.getCode()))));
//        //        街道
////        List list3 = new ArrayList<>();
////        json3.forEach(item->{
////            list3.add(item.getCode());
////        });
////        List<DbCity> json4 = dbCityMapper.selectList(new QueryWrapper<DbCity>().eq("lev", 4).in("parent_code", list3));
////        Map<String,List<DbCity>> mdv3=json4.stream().collect(Collectors.groupingBy(DbCity::getParentCode));
////
////
////        json.stream().filter(a -> a.getChildren() != null).forEach(county -> {
////            List<DbCity> children = county.getChildren();
////            children.forEach(town -> {
////                List<DbCity> townships = mdv3.get(town.getCode());
////                if (townships != null) {
////                    town.setChildren(townships);
////                }
////            });
////        });
//        return json;
//    }
}
